package com.test.appotasdk;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.appota.asdk.inapp.InappAPI;

public class ListItemActivity extends Activity {
	
	private String type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_list_item);
		type = getIntent().getStringExtra("type");
		ListView lv = (ListView) findViewById(R.id.listview);
		List<Item> items = new ArrayList<ListItemActivity.Item>();
		if(type.equalsIgnoreCase("sms")){
			items.add(new Item("20 Coins", "10.000 VND", 10000, R.drawable.icon_money));
			items.add(new Item("30 Coins", "15.000 VND", 15000, R.drawable.icon_money));
		} else if(type.equalsIgnoreCase("card")){
			items.add(new Item("20 Coins", "10.000 VND", 10000, R.drawable.icon_money));
			items.add(new Item("40 Coins", "20.000 VND", 20000, R.drawable.icon_money));
			items.add(new Item("100 Coins", "50.000 VND", 50000, R.drawable.icon_money));
			items.add(new Item("200 Coins", "100.000 VND", 100000, R.drawable.icon_money));
		} else if(type.equalsIgnoreCase("bank")){
			items.add(new Item("40 Coins", "20.000 VND", 20000, R.drawable.icon_money));
			items.add(new Item("100 Coins", "50.000 VND", 50000, R.drawable.icon_money));
			items.add(new Item("200 Coins", "100.000 VND", 100000, R.drawable.icon_money));
		} else {
			items.add(new Item("40 Coins", "$0.99", 0.99, R.drawable.icon_money));
			items.add(new Item("200 Coins", "$4.99", 4.99, R.drawable.icon_money));
			items.add(new Item("400 Coins", "$9.99", 9.99, R.drawable.icon_money));
		}
		ItemAdapter adapter = new ItemAdapter(this, R.layout.item_row, items);
		lv.setAdapter(adapter);
	}

	private class ItemAdapter extends ArrayAdapter<Item> {

		public ItemAdapter(Context context, int textViewResourceId, List<Item> objects) {
			super(context, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.item_row, null);
				holder = new ViewHolder();
				holder.btnBuy = (Button) convertView.findViewById(R.id.btn_buy);
				holder.icon = (ImageView) convertView.findViewById(R.id.icon);
				holder.name = (TextView) convertView.findViewById(R.id.text_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final Item item = getItem(position);
			holder.btnBuy.setText(item.getPriceStr());
			holder.icon.setImageResource(item.getIconRes());
			holder.name.setText(item.getName());
			holder.btnBuy.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(type.equalsIgnoreCase("sms")){
						InappAPI.getInstance().setContext(ListItemActivity.this).inappSMS((int)item.getPrice(), "http://filestore9.com/test.php", null, "911-0029257", false);
					} else if(type.equalsIgnoreCase("card")){
						InappAPI.getInstance().setContext(ListItemActivity.this).inAppCard("http://filestore9.com/test.php", null, "911-0029257", 1);
					} else if(type.equalsIgnoreCase("bank")){
						InappAPI.getInstance().setContext(ListItemActivity.this).inAppBank((int)item.getPrice(), null, null, null);
					} else {
						InappAPI.getInstance().setContext(ListItemActivity.this).inAppPaypal(item.getPrice(), null, null, null);
					}
				}
			});
			return convertView;
		}
	}
	
	private class ViewHolder {
		ImageView icon;
		TextView name;
		Button btnBuy;
	}

	private class Item {
		
		public Item(String name, String priceStr, double price, int iconRes) {
			super();
			this.name = name;
			this.priceStr = priceStr;
			this.price = price;
			this.iconRes = iconRes;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPriceStr() {
			return priceStr;
		}

		public void setPriceStr(String priceStr) {
			this.priceStr = priceStr;
		}

		public double getPrice() {
			return price;
		}

		public void setPrice(double price) {
			this.price = price;
		}

		public int getIconRes() {
			return iconRes;
		}

		public void setIconRes(int iconRes) {
			this.iconRes = iconRes;
		}

		private String name;
		private String priceStr;
		private double price;
		private int iconRes;

	}
}
